﻿<?php
	$mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="antsc"; // 连接数据库用户名
    $mysql_password="fjh0FJH"; // 连接数据库密码
    $mysql_database="antsc_cms"; // 数据库的名字
    
    // 连接到数据库
	
	$resultXML = "";
	
    $conn=mysql_connect($mysql_server_name, $mysql_username,$mysql_password);
	mysql_query("set names 'utf8'");
    // 从表中提取信息的sql语句
    $strsql="SELECT id,label FROM page ORDER BY id";
    // 执行sql查询
    $result=mysql_db_query($mysql_database, $strsql, $conn);   // 获取查询结果
    $row = mysql_fetch_row($result);
	if (mysql_num_rows($result) > 0) {
		mysql_data_seek($result, 0);
	}
	$i = 0;
    while ($row=mysql_fetch_row($result))
    {
        $pageArr[$i] = "$row[0]";
		$pageLabelArr[$i] = "$row[1]";
		$i++;
		
    }
	mysql_free_result($result);
	
	for ($i = 0; $i < 1; $i++) {
	//---------------text
		$strsql="SELECT containerLabel FROM text where pageID=".$pageArr[$i]." GROUP BY containerLabel ORDER BY timestamp ";
		// 执行sql查询
		$result=mysql_db_query($mysql_database, $strsql, $conn);   // 获取查询结果
		$row = mysql_fetch_row($result);
		if (mysql_num_rows($result) > 0) {
			mysql_data_seek($result, 0);
		}
		$j = 0;
		while ($row=mysql_fetch_row($result))
		{
			$textContainerLabelArr[$i][$j]= "$row[0]";
			$j++;
		}
		mysql_free_result($result); 
	//==================img	
		$strsql="SELECT containerLabel FROM img where pageID=".$pageArr[$i]." GROUP BY containerLabel ORDER BY timestamp ";
		// 执行sql查询
		$result=mysql_db_query($mysql_database, $strsql, $conn);   // 获取查询结果
		$row = mysql_fetch_row($result);
		if (mysql_num_rows($result) > 0) {
			mysql_data_seek($result, 0);
		}
		$j = 0;
		while ($row=mysql_fetch_row($result))
		{
			$imgContainerLabelArr[$i][$j]= "$row[0]";
			$j++;
			
		}
		mysql_free_result($result); 
		
		$strsql="SELECT containerLabel FROM music where pageID=".$pageArr[$i]." GROUP BY containerLabel ORDER BY timestamp ";
		// 执行sql查询
		$result=mysql_db_query($mysql_database, $strsql, $conn);   // 获取查询结果
		$row = mysql_fetch_row($result);
		if (mysql_num_rows($result) > 0) {
			mysql_data_seek($result, 0);
		}
		$j = 0;
		while ($row=mysql_fetch_row($result))
		{
			$musicContainerLabelArr[$i][$j]= "$row[0]";
			$j++;
			
		}
		mysql_free_result($result); 
		
		$strsql="SELECT containerLabel FROM video where pageID=".$pageArr[$i]." GROUP BY containerLabel ORDER BY timestamp ";
		// 执行sql查询
		$result=mysql_db_query($mysql_database, $strsql, $conn);   // 获取查询结果
		$row = mysql_fetch_row($result);
		if (mysql_num_rows($result) > 0) {
			mysql_data_seek($result, 0);
		}
		$j = 0;
		while ($row=mysql_fetch_row($result))
		{
			$videoContainerLabelArr[$i][$j]= "$row[0]";
			$j++;
			
		}
		mysql_free_result($result); 
		
		$strsql="SELECT containerLabel FROM file where pageID=".$pageArr[$i]." GROUP BY containerLabel ORDER BY timestamp ";
		// 执行sql查询
		$result=mysql_db_query($mysql_database, $strsql, $conn);   // 获取查询结果
		$row = mysql_fetch_row($result);
		if (mysql_num_rows($result) > 0) {
			mysql_data_seek($result, 0);
		}
		$j = 0;
		while ($row=mysql_fetch_row($result))
		{
			$fileContainerLabelArr[$i][$j]= "$row[0]";
			$j++;
			
		}
		mysql_free_result($result); 
	}
	
	$resultXML.= "<data><site label='网站'>";
	
	for ($i = 0; $i < count($pageArr); $i++) {
		$resultXML.= "<page id='".$pageArr[$i]."' label='".$pageLabelArr[$i]."' >";
		if (count($textContainerLabelArr[$i]) > 0) {
			$resultXML.= "<textContainer>";
		}
		
		for ($j = 0; $j < count($textContainerLabelArr[$i]); $j++) {
			$strsql="SELECT * FROM text where pageID=".$pageArr[$i]." AND containerLabel='".$textContainerLabelArr[$i][$j]."' GROUP BY containerLabel ORDER BY timestamp ";
			
			$result=mysql_db_query($mysql_database, $strsql, $conn);
			$row = mysql_fetch_row($result);
			if (mysql_num_rows($result) > 0) {
				mysql_data_seek($result, 0);
			}
			$resultXML.= "<texts label='".$textContainerLabelArr[$i][$j]."' >";
			while ($row=mysql_fetch_row($result))
			{
				$resultXML.= "<text ";
				 for ($k=0; $k<mysql_num_fields($result); $k++)
				{
					$resultXML.= mysql_field_name($result, $k);
					$resultXML.= "='";
					$resultXML.= "$row[$k]";
					$resultXML.= "' ";
				}
				$resultXML.= " />";			
				$j++;				
			}
			mysql_free_result($result); 
			$resultXML.= "</texts>";
		}
		if (count($textContainerLabelArr[$i]) > 0) {
			$resultXML.= "</textContainer>";
		}
		
		
		
		if (count($imgContainerLabelArr[$i]) > 0) {
			$resultXML.= "<imgContainer>";
		}
		
		
		for ($j = 0; $j < count($imgContainerLabelArr[$i]); $j++) {
			$strsql="SELECT * FROM img where pageID=".$pageArr[$i]." AND containerLabel='".$imgContainerLabelArr[$i][$j]."' GROUP BY containerLabel ORDER BY timestamp ";
			
			$result=mysql_db_query($mysql_database, $strsql, $conn);
			$row = mysql_fetch_row($result);
			if (mysql_num_rows($result) > 0) {
				mysql_data_seek($result, 0);
			}
			$resultXML.= "<imgs label='".$imgContainerLabelArr[$i][$j]."' >";
			while ($row=mysql_fetch_row($result))
			{
				$resultXML.= "<img ";
				 for ($k=0; $k<mysql_num_fields($result); $k++)
				{
					$resultXML.= mysql_field_name($result, $k);
					$resultXML.= "='";
					$resultXML.= "$row[$k]";
					$resultXML.= "' ";
				}
				$resultXML.= " />";			
				$j++;				
			}
			mysql_free_result($result); 
			$resultXML.= "</imgs>";
		}
		if (count($imgContainerLabelArr[$i]) > 0) {
			$resultXML.= "</imgContainer>";
		}
		
		
		if (count($musicContainerLabelArr[$i]) > 0) {
			$resultXML.= "<musicContainer>";
		}
		
		for ($j = 0; $j < count($musicContainerLabelArr[$i]); $j++) {
			$strsql="SELECT * FROM music where pageID=".$pageArr[$i]." AND containerLabel='".$musicContainerLabelArr[$i][$j]."' GROUP BY containerLabel ORDER BY timestamp ";
			
			$result=mysql_db_query($mysql_database, $strsql, $conn);
			$row = mysql_fetch_row($result);
			if (mysql_num_rows($result) > 0) {
				mysql_data_seek($result, 0);
			}
			$resultXML.= "<musics label='".$musicContainerLabelArr[$i][$j]."' >";
			while ($row=mysql_fetch_row($result))
			{
				$resultXML.= "<music ";
				 for ($k=0; $k<mysql_num_fields($result); $k++)
				{
					$resultXML.= mysql_field_name($result, $k);
					$resultXML.= "='";
					$resultXML.= "$row[$k]";
					$resultXML.= "' ";
				}
				$resultXML.= " />";			
				$j++;				
			}
			mysql_free_result($result); 
			
			$resultXML.= "</musics>";
			
		}
		if (count($musicContainerLabelArr[$i]) > 0) {
			$resultXML.= "</musicContainer>";
		}
		
		
		if (count($videoContainerLabelArr[$i]) > 0) {
			$resultXML.= "<videoContainer>";
		}
		
		for ($j = 0; $j < count($videoContainerLabelArr[$i]); $j++) {
			$strsql="SELECT * FROM video where pageID=".$pageArr[$i]." AND containerLabel='".$videoContainerLabelArr[$i][$j]."' GROUP BY containerLabel ORDER BY timestamp ";
			
			$result=mysql_db_query($mysql_database, $strsql, $conn);
			$row = mysql_fetch_row($result);
			if (mysql_num_rows($result) > 0) {
				mysql_data_seek($result, 0);
			}
			$resultXML.= "<videos label='".$videoContainerLabelArr[$i][$j]."' >";
			while ($row=mysql_fetch_row($result))
			{
				$resultXML.= "<video ";
				 for ($k=0; $k<mysql_num_fields($result); $k++)
				{
					$resultXML.= mysql_field_name($result, $k);
					$resultXML.= "='";
					$resultXML.= "$row[$k]";
					$resultXML.= "' ";
				}
				$resultXML.= " />";			
				$j++;				
			}
			mysql_free_result($result); 
			$resultXML.= "</videos>";
		}
		if (count($videoContainerLabelArr[$i]) > 0) {
			$resultXML.= "</videoContainer>";
		}
		
		if (count($fileContainerLabelArr[$i]) > 0) {
			$resultXML.= "<fileContainer>";
		}
		
		for ($j = 0; $j < count($fileContainerLabelArr[$i]); $j++) {
			$strsql="SELECT * FROM file where pageID=".$pageArr[$i]." AND containerLabel='".$fileContainerLabelArr[$i][$j]."' GROUP BY containerLabel ORDER BY timestamp ";
			
			$result=mysql_db_query($mysql_database, $strsql, $conn);
			$row = mysql_fetch_row($result);
			if (mysql_num_rows($result) > 0) {
				mysql_data_seek($result, 0);
			}
			$resultXML.= "<files label='".$fileContainerLabelArr[$i][$j]."' >";
			while ($row=mysql_fetch_row($result))
			{
				$resultXML.= "<file ";
				 for ($k=0; $k<mysql_num_fields($result); $k++)
				{
					$resultXML.= mysql_field_name($result, $k);
					$resultXML.= "='";
					$resultXML.= "$row[$k]";
					$resultXML.= "' ";
				}
				$resultXML.= "/>";			
				$j++;				
			}
			mysql_free_result($result); 
			$resultXML.= "</files>";
		}
		if (count($fileContainerLabelArr[$i]) > 0) {
			$resultXML.= "</fileContainer>";
		}		
		
		
		$resultXML.= "</page>";
	}
	$resultXML.= "</site></data>";
	mysql_close();  
	
	
	
	
	
	//$resultXML.= "\r\n";
	
	$file_name = "../xml/data.xml"; 
	// 绝对路径: homedata.dat 
	 
	$file_pointer = fopen($file_name, "w+"); 
	// "w"是一种模式，详见后面
	 
	fwrite($file_pointer, $resultXML); 
	// 先把文件剪切为0字节大小， 然后写入
	 
	fclose($file_pointer); 
   
    // 关闭连接
    





?>